<?php
	include("connect.php");
	$css = '<link rel="stylesheet" type="text/css" href="asset/css/class_admin.css" />';
	$css .= '<link rel="stylesheet" type="text/css" href="asset/css/niceforms-default.css" />';
	$css .= '<script language="javascript" type="text/javascript" src="asset/js/niceforms.js"></script>';
	$css .= '<script language="javascript" type="text/javascript" src="asset/js/datetimepicker.js"></script>';
	$css .= '<link rel="stylesheet" type="text/css" media="all" href="asset/css/paging.css">';
	$title = 'Admin List Book';
	$page_name = 'admin_bookonsale';
	$page = 1;
	
$sql = "select user_category from m_user where user_id=".$_SESSION['userid'];
$retval = mysql_query( $sql, $con );
while($row = mysql_fetch_array($retval))
	{
		$user_category = $row[0];
	}
if(isset($_SESSION["userid"]))
	{
		if($user_category ==9)
		{
			include("header_loggedinadmin.php");
		}else
		{
			include("header_loggedin.php");
		}
	}
else
{
	include("header.php");
}
//check category

if($user_category !=9)
{
	echo '<script>';
		echo "alert('You can not access this page!');";
		echo 'window.location = "index.php"';
		echo '</script>';
}	
?>
<?php
//paging

	$targetpage = $page_name.".php"; 	
	$limit = 9; 
	$sql = "SELECT count(*) FROM t_user_book a";
						$sql .= " INNER JOIN m_book b ON a.book_id = b.book_id";
						$sql .= " INNER JOIN m_user c ON a.user_id = c.user_id";
						if(isset($_REQUEST['tanggal1']) AND isset($_REQUEST['tanggal2']))
						{
							if($_REQUEST['tanggal1'] != "" AND $_REQUEST['tanggal2'] != "")
							{
								$tanggal_1 = $_REQUEST['tanggal1'];
								$tanggal_2 = $_REQUEST['tanggal2'];
								$sql .= " WHERE DATE_FORMAT(a.date_uploaded, '%d-%m-%Y') >= '".$tanggal_1."'";
								$sql .= " AND DATE_FORMAT(a.date_uploaded, '%d-%m-%Y') <= '".$tanggal_2."'";
							}
						}
						$sql .= " ORDER BY a.date_uploaded DESC";
						$result = mysql_query($sql);
    while($row = mysql_fetch_array($result))
	{
		$total_pages =$row[0];
	}
	
	//$total_pages = mysql_fetch_array(mysql_query($query));
	//$total_pages = $total_pages[num];
	$stages = 3;
	$page = mysql_escape_string($_GET['page']);
	if($page){
		$start = ($page - 1) * $limit; 
	}else{
		$start = 0;	
		}
	
$sql = "SELECT c.fname, b.book_title, b.isbn, a.date_uploaded, a.is_sold, a.user_book_id FROM t_user_book a";
						$sql .= " INNER JOIN m_book b ON a.book_id = b.book_id";
						$sql .= " INNER JOIN m_user c ON a.user_id = c.user_id";
						if(isset($_REQUEST['tanggal1']) AND isset($_REQUEST['tanggal2']))
						{
							if($_REQUEST['tanggal1'] != "" AND $_REQUEST['tanggal2'] != "")
							{
								$tanggal_1 = $_REQUEST['tanggal1'];
								$tanggal_2 = $_REQUEST['tanggal2'];
								$sql .= " WHERE DATE_FORMAT(a.date_uploaded, '%d-%m-%Y') >= '".$tanggal_1."'";
								$sql .= " AND DATE_FORMAT(a.date_uploaded, '%d-%m-%Y') <= '".$tanggal_2."'";
								$pagingadd .= '&tanggal1='.$tanggal1.'&tanggal2='.$tanggal2;

							}
						}
						$sql .= " ORDER BY a.date_uploaded DESC";
						if (!mysql_query($sql,$con))
						{
							
							die('<br><br>Query execution error: ' . mysql_error());
						}
						//echo $sql;
						
$sql .= " LIMIT $start, $limit";
	$result = mysql_query($sql);
	
	// Initial page num setup
	if ($page == 0){$page = 1;}
	$prev = $page - 1;	
	$next = $page + 1;							
	$lastpage = ceil($total_pages/$limit);		
	$LastPagem1 = $lastpage - 1;	
	
	$paginate = '';
	include("paging.php");

?>
<div id="blue_bg">
	<div id="main">
        <div class="wrapper">
        	<div id="content">
            	<div id="white_box">
                	<h3 class="flag_long">Book on Sale</h3>
                    <p>
                    	<form name="show">
                    	show book from <input type="Text" name="tanggal1" id="tanggal1" maxlength="20" size="20"><a href="javascript:NewCal('tanggal1','ddmmyyyy',false,24)"><img src="asset/img/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a>&nbsp; to <input type="Text" name="tanggal2" id="tanggal2" maxlength="20" size="20"><a href="javascript:NewCal('tanggal2','ddmmyyyy',false,24)"><img src="asset/img/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a> &nbsp;<input type="submit" value="SHOW" id="show_btn" />
                        </form>
                    </p>
                    <table id="rounded-corner" summary="Book On Sale BookSquare">
                        <thead>
                            <tr>
                                <th scope="col" class="rounded-company"></th>
                                <th scope="col" class="rounded">Uploader</th>
                                <th scope="col" class="rounded">Book Name</th>
                                <th scope="col" class="rounded">ISBN</th>
                                <th scope="col" class="rounded">Date Uploaded</th>
                                <th scope="col" class="rounded">Sold</th>
                                <th scope="col" class="rounded-q4">Detail</th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <td colspan="6" class="rounded-foot-left"><!--<em>The book that is blocked will be highlight in <span class="blocked">red</span></em>--></td>
                                <td class="rounded-foot-right">&nbsp;</td>
                    
                            </tr>
                        </tfoot>
                        <tbody>
                        <?php
						
						while($row = mysql_fetch_array($result))
						{
							if($row['is_sold'] == 0)
								$status_sold = "not sold";
							else
								$status_sold = "sold";
							echo '
                            <tr>
                                <td><input type="checkbox" name="" /></td>
                                <td>'.$row['fname'].'</td>
                                <td>'.$row['book_title'].'</td>
                                <td>'.$row['isbn'].'</td>
                                <td>'.$row['date_uploaded'].'</td>
                                <td>'.$status_sold.'</td>
                                <td><a href="book_detail.php?userbookid='.$row['user_book_id'].'" class="ask">detail</a></td>
                            </tr>
							';
						}
                        ?>
                        <!--    <tr>
                                <td><input type="checkbox" name="" /></td>
                                <td><span class="blocked">Laurentia</span></td>
                                <td><span class="blocked">Human Computer Interaction</span></td>
                                <td><span class="blocked">1234-5678-910</span></td>
                                <td><span class="blocked">12/05/2010</span></td>
                                <td><span class="blocked">sold</span></td>
                                <td><a href="#" class="ask">detail</a></td>
                            </tr> 
                            
                            <tr>
                                <td><input type="checkbox" name="" /></td>
                                <td>Laurentia</td>
                                <td>Human Computer Interaction</td>
                                <td>1234-5678-910</td>
                                <td>12/05/2010</td>
                                <td>sold</td>
                                <td><a href="#" class="ask">detail</a></td>
                            </tr>
                            
                            <tr>
                                <td><input type="checkbox" name="" /></td>
                                <td>Laurentia</td>
                                <td>Human Computer Interaction</td>
                                <td>1234-5678-910</td>
                                <td>12/05/2010</td>
                                <td>sold</td>
                                <td><a href="#" class="ask">detail</a></td>
                            </tr>  
                            <tr>
                                <td><input type="checkbox" name="" /></td>
                                <td>Laurentia</td>
                                <td>Human Computer Interaction</td>
                                <td>1234-5678-910</td>
                                <td>12/05/2010</td>
                                <td>sold</td>
                                <td><a href="#" class="ask">detail</a></td>
                            </tr>
                            
                            <tr>
                                <td><input type="checkbox" name="" /></td>
                                <td>Laurentia</td>
                                <td>Human Computer Interaction</td>
                                <td>1234-5678-910</td>
                                <td>12/05/2010</td>
                                <td>sold</td>
                                <td><a href="#" class="ask">detail</a></td>
                            </tr>    
                            -->
                        </tbody>
                    </table>
                    
                    
                    <div class="pagination">
                   <?php echo $paginate; ?>
                    </div> 
                    <div class="clear"></div>
           		</div>
            </div>
        </div>
    </div>
</div>

<?php
 include("footer.php");
?>